package com.lemon.core.base.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 统一响应实体
 *
 * @author pan
 * @since 2018-03-16
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageResult<T> implements Serializable {

    @ApiModelProperty(value = "响应码", required = true)
    private int code;

    @ApiModelProperty(value = "响应消息", required = true)
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private T data;

    public static <T> PageResult<PageInfo<T>> page(List<T> data, Long total) {
        return page(new PageInfo<>(data, total));
    }

    public static <T> PageResult<PageInfo<T>> page(PageInfo<T> pageInfo) {
        PageResult<PageInfo<T>> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(pageInfo);
        return result;
    }

    public static boolean isSuccess(PageResult<?> result) {
        return result != null && ResultCode.SUCCESS.getCode() == result.getCode();
    }
}
